$(function () {
    //常用jQuery对象
    var outcomeManage_datagrid = $("#outcomeManage_list");
    var outcomeManage_dailog = $("#outcomeManage_dialog");
    var outcomeManage_form = $("#outcomeManage_form");
    var resetPassword_dialog = $("#resetPassword_dialog");

    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {

            outcomeManage_dailog.dialog("open");
            outcomeManage_dailog.dialog("setTitle", "新增");
            $("#password").show();
        },

        //编辑按钮方法
        edit: function () {
            var row = outcomeManage_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }

            if (row.payUser) {
                row["payUser.id"] = row.payUser.id;
            }
            if (row.handlerUser) {
                row["handlerUser.id"] = row.handlerUser.id;
            }
            if (row.payType) {
                row["payType.id"] = row.payType.id;
            }
            if (row.payStyle) {
                row["payStyle.id"] = row.payStyle.id;
            }
            if (row.clz) {
                row["clz.id"] = row.clz.id;
            }
            outcomeManage_dailog.dialog("setTitle", "编辑");
            outcomeManage_dailog.form("load", row);

            outcomeManage_dailog.dialog("open");


        },

        //导出文件模板
        getImportXlsMode:function () {

            window.open("/outcomeManage/getImportXlsMode.do");
        },
        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            outcomeManage_form.form("submit", {
                url: "/outcomeManage/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            outcomeManage_dailog.dialog("close");
                            outcomeManage_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            outcomeManage_dailog.dialog("close");
                        });
                    }
                }
            })
        },

        //刷新页面
        reloadList: function () {
            $("#keyword").textbox("setValue", "");
            $("#deptId").combobox("setValue", "");
            var beginDate = $("#beginDate").datebox("setValue", "");
            var endDate = $("#endDate").datebox("setValue", "");
            outcomeManage_datagrid.datagrid("reload", {deptId: "-1"});
        },


        //高级查询按钮
        searchs: function () {
            var payTypeId = $("#payTypeId").textbox("getValue");
            var classId = $("#classId").combobox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");
            outcomeManage_datagrid.datagrid("load", {
                payTypeId: payTypeId,
                classId: classId,
                beginDate: beginDate,
                endDate: endDate
            });
        },

        //删除一条数据的弹窗
        del:function () {
            var row = outcomeManage_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示', "确定要删除这条明细吗？", function (r) {
                if (r) {
                    $.get("/outcomeManage/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                outcomeManage_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });
        },

        //审核按钮
        auditor:function () {
            var row = outcomeManage_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示', "确定要审核这条冚家铲吗？", function (r) {
                if (r) {
                    $.get("/outcomeManage/auditor.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                outcomeManage_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });
        }
    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    outcomeManage_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        url: "/outcomeManage/query.do",
        singleSelect: "true",
        pagination: true,
        rownumbers: true,
        columns: [[
            {field: 'hiredate', title: '日期', width: 100},
            {field: 'payMoney', title: '支付金额', width: 100},
            {field: 'digest', title: '摘要', width: 100},
            {
                field: 'payUser', title: '支出人', width: 100, formatter: function (payUser) {
                    return payUser ? payUser.username : '';
                }
            },
            {
                field: 'handlerUser', title: '经手人', width: 100, formatter: function (handlerUser) {
                    return handlerUser ? handlerUser.username : '';
                }
            },
            {
                field: 'payType', title: '支付方式', width: 100, formatter: function (payType) {
                    return payType ? payType.name : '';
                }
            },
            {
                field: 'payStyle', title: '花费类型', width: 100, formatter: function (payStyle) {
                    return payStyle ? payStyle.name : '';
                }
            },
            {field: 'receipt', title: '单据号', width: 100},
            {
                field: 'clz', title: '所属班级', width: 100, formatter: function (clz) {
                    return clz ? clz.name : '';
                }
            },
            {
                field: 'auditor', title: '审核人', width: 100, formatter: function (auditor) {
                    return auditor ? auditor.username : auditor;
                }
            },
            {
                field: 'state', title: '状态', width: 100, formatter: function (state) {
                    return state ? '已审核' : "<font color='red'>未审核</font>";
                }
            }

        ]],
        toolbar: "#tools",
        onClickRow: function (index, row) {
            if (row.state) {
                $("#btn_edit").hide();
                $("#btn_del").hide();
                $("#btn_auditor").linkbutton({text:"取消审核"});
            } else {
                $("#btn_edit").show();
                $("#btn_del").show();
                $("#btn_auditor").linkbutton({text:"审核"});

            }
        },
    });

    //编辑添加弹出框设置
    outcomeManage_dailog.dialog({
        width: 350,
        height: 400,
        closed: true,
        onClose: function () {
            outcomeManage_form.form("clear");
        },
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["saveOrUpdate"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                outcomeManage_dailog.dialog("close");
            }
        }]
    });


    //form表单设置
    outcomeManage_dailog.form({
        fit: true
    })

    //新增默认部门设置
    $("#depts").combobox("select", "1");

});

